package com.tcm.content.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.base.core.exception.ServiceException;
import com.base.core.utils.ListUtils;
import com.base.core.utils.R;
import com.base.genid.generator.UidGenerator;
import com.base.redis.JedisClient;
import com.tcm.content.dao.mapper.PanelNavMapper;
import com.tcm.content.dto.PanelNavDTO;
import com.tcm.content.entity.PanelNav;
import com.tcm.content.enums.BusinessCodeEnum;
import com.tcm.content.service.PanelNavService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.List;
import java.util.stream.Collectors;

@DubboService(version = "0.0.1-alpha", delay = -1, retries = 3)
public class PanelNavServiceImpl implements PanelNavService {

    private final static Logger log = LoggerFactory.getLogger(PanelNavServiceImpl.class);

    @Autowired
    private PanelNavMapper tbPanelContentMapper;

    @Autowired
    private JedisClient jedisClient;

    @DubboReference(version = "0.0.1-beta", group = "cached-uid", timeout = 6000, check = false, retries = 3)
    private UidGenerator uidGenerator;


    @Value("${NAV_PANEL_ID}")
    private String NAV_PANEL_ID;

    @Override
    public String getNavPanelId(){
        return NAV_PANEL_ID;
    }

    @Override
    public int addPanelContent(PanelNavDTO panelContent) throws ServiceException {
        PanelNav tbPanelContent = new PanelNav();
        BeanUtils.copyProperties(panelContent, tbPanelContent);
        tbPanelContent.setId(uidGenerator.getUID());
        tbPanelContent.setCreateTime(DateUtil.date());
        tbPanelContent.setUpdateTime(DateUtil.date());
        tbPanelContent.setStatus(new Byte("1"));
        if (tbPanelContentMapper.insert(tbPanelContent) != 1) {
            R.throwFail(BusinessCodeEnum.PANEL_ADD_FAIL);
        }
        //同步缓存
        deleteHomeRedis();
        return 1;
    }

    @Override
    public List<PanelNavDTO> listPanelContentByPanelId(Long panelId, Integer length) {
       List<PanelNav> panelNavs = tbPanelContentMapper.listPanelNav(panelId, length);
       if(ListUtils.isEmpty(panelNavs)){
           return null;
       }
       return panelNavs.stream().map(panelNav -> {
           PanelNavDTO panelNavDTO = new PanelNavDTO();
           BeanUtils.copyProperties(panelNav, panelNavDTO);
           return panelNavDTO;
       }).collect(Collectors.toList());
    }

    @Override
    public int deletePanelContent(Long id) throws ServiceException {
        if (tbPanelContentMapper.delete(id) != 1) {
            R.throwFail(BusinessCodeEnum.PANEL_DELETE_FAIL);
        }
        //同步缓存
        deleteHomeRedis();
        return 1;
    }

    @Override
    public int updateContent(PanelNavDTO panelContent) throws ServiceException {
        PanelNav tbPanelContent = new PanelNav();
        BeanUtils.copyProperties(panelContent, tbPanelContent);
        PanelNavDTO old = findPanelContentById(panelContent.getId());
        if(ObjectUtil.isEmpty(old)){
            R.throwFail(BusinessCodeEnum.PANEL_UPDATE_FAIL);
        }
        tbPanelContent.setUpdateTime(DateUtil.date());
        if (tbPanelContentMapper.update(tbPanelContent) != 1) {
            R.throwFail(BusinessCodeEnum.PANEL_UPDATE_FAIL);
        }
        //同步缓存
        deleteHomeRedis();
        return 1;
    }

    @Override
    public PanelNavDTO findPanelContentById(Long id) {
        PanelNav tbPanelContent = tbPanelContentMapper.findById(id);
        if (tbPanelContent == null) {
            R.throwFail(BusinessCodeEnum.FAIL_FIND_PANEL_SLIDE_BY_ID);
        }
        PanelNavDTO panelContent = new PanelNavDTO();
        BeanUtils.copyProperties(tbPanelContent, panelContent);
        return panelContent;
    }

    @Override
    public String findNavRedis() {
        try {
            String json = jedisClient.get(NAV_PANEL_ID);
            return json;
        } catch (Exception e) {
            log.error(e.toString());
        }
        return "";
    }

    @Override
    public int updateNavRedis() {
        try {
            jedisClient.del(NAV_PANEL_ID);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 1;
    }

    /**
     * 同步首页缓存
     */
    public void deleteHomeRedis() {
        try {
            jedisClient.del(NAV_PANEL_ID);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
